import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:getwidget/components/animation/gf_animation.dart';
import 'package:getwidget/types/gf_animation_type.dart';

import '../controllers/gfanimation_controller.dart';

class GfanimationView extends StatefulWidget {
  GfanimationView({Key? key}) : super(key: key);

  @override
  _GfanimationViewState createState() => _GfanimationViewState();
}

class _GfanimationViewState extends State<GfanimationView> with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();

    // 1.创建 AnimationController
    controller = AnimationController(
      duration: const Duration(seconds: 5), 
      vsync: this
    );

    // 2. 声明动画曲线
    animation = new CurvedAnimation(
      parent: controller, 
      curve: Curves.linear
    );

    // 3. 反复执行动画
    controller.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('动画'),
        centerTitle: true,
        // elevation: 0,
      ),
      body: Container(
        margin: EdgeInsets.all(10),
        color: Colors.white,
        child: ListView(
          children: [
            GestureDetector(
              onTap: () {
                controller.repeat();
              },
              child: GFAnimation(
                turnsAnimation: animation,
                controller: controller,
                type: GFAnimationType.rotateTransition,
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/images/flutter.jpg',
                  width: 120,
                  height: 120,
                ),
              ),
            ),
            SizedBox(height: 30,),

            GestureDetector(
              onTap: () {
                controller.repeat();
              },
              child: GFAnimation(
                turnsAnimation: animation,
                controller: controller,
                type: GFAnimationType.scaleTransition,
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/images/cliu.jpg',
                  width: 120,
                  height: 120,
                ),
              ),
            ),
            SizedBox(height: 30,),

            // GestureDetector(
            //   onTap: () {
            //     controller.repeat();
            //   },
            //   child: GFAnimation(
            //     duration: Duration(seconds: 2),
            //     type: GFAnimationType.align,
            //     alignment: Alignment.bottomLeft,
            //     child: Image.asset(
            //       'assets/images/flutter.jpg',
            //       width: 120,
            //       height: 120,
            //     ),
            //   ),
            // ),
          ],
        ),
      )
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

